#============================================================
#	Plugin
#============================================================
# @datetime: 2022-5-2 19:01:12
#============================================================
class_name _CodeEditor_SubPlugin_NodePath
extends _CodeEditor_SubPluginBase


const Dialog = preload("NodePathDialog.tscn")

var dialog = Dialog.instance()


#============================================================
#   自定义
#============================================================
#(override)
func install():
	.install()
	add_editor_node(dialog)
	dialog.connect("click_ok", self, "add_node_path")


#(override)
func init_menu_button():
	.init_menu_button()
	_menu_button.set_item_shortcut(
		_menu_button.add_popup_item("添加选中的节点变量", self, "add_node_var")
		, get_util() \
			.parse_shortcut_util \
			.parse_shortcut("Ctrl + Shift + P")
	)
	_menu_button.add_popup_item("添加 NodePath 变量", self, "popup_dialog")


#(override)
func uninstall():
	.uninstall()
	dialog.queue_free()


##  找到这个脚本的节点
## @parent  
## @script  
## @return  
func _find_script_node(parent: Node, script: Script) -> Node:
	for child in parent.get_children():
		if _node_is_script(child, script):
			return child 
	var node : Node = null
	for child in parent.get_children():
		node = _find_script_node(child, script)
		if node:
			break
	return node


## 这个节点是这个脚本类
func _node_is_script(node: Node, script: Script) -> bool:
	return node.get_script() == script


## 添加节点变量
func add_node_var():
	var select_nodes = get_editor_interface().get_selection().get_selected_nodes()
	if select_nodes.size() == 0:
		return
	var scene_node : Node = get_editor_interface().get_edited_scene_root()
	if select_nodes.size() == 1:
		if select_nodes[0] == scene_node:
			printerr("选中的节点是当前场景根节点，没有添加")
			return
	
	# 当前脚本的节点
	var current_script : Script = get_util() \
			.script_editor_util \
			.get_current_script()
	var script_node : Node 
	if _node_is_script(scene_node, current_script):
		script_node = scene_node
	else:
		script_node = _find_script_node(scene_node, current_script)
	if script_node == null:
		printerr("添加失败，场景中没有这个脚本的节点")
		return
	
	# 添加时不添加当前脚本的节点的路径
	select_nodes.erase(script_node)
	
	var textedit = get_util().script_editor_util.get_current_code_textedit()
	if textedit == null:
		printerr("当前没有打开代码编辑器")
	
	var code = ""
	# 光标在行头部
	if textedit.cursor_get_column() == 0:
		for node in select_nodes:
			var node_path : String = script_node.get_path_to(node)
			# 生成的变量代码
	#		code += "onready var _{varname} := get_node(\"{path}\") as {type}\n".format({
	#			"path": node_path,
	#			"varname": node.name.capitalize().to_lower().replace(" ", "_"),
	#			"type": node.get_class(),
	#		})
			code += "onready var _{varname} = get_node(\"{path}\") \n".format({
				"path": node_path,
				"varname": node.name.capitalize().to_lower().replace(" ", "_"),
			})
		textedit.cursor_set_column(0)
	
	# 光标在向后缩进位置
	else:
		# 获取缩进字符
		var linetext : String = textedit.get_line(textedit.cursor_get_line())
		var indent_text : String = linetext.left(textedit.cursor_get_column())
		
		for node in select_nodes:
			var node_path : String = script_node.get_path_to(node)
			# 生成的变量代码
			code += "{indent_text}var {varname} = get_node(\"{path}\") \n".format({
				"indent_text": indent_text,
				"path": node_path,
				"varname": node.name.capitalize().to_lower().replace(" ", "_"),
			})
	
	textedit.insert_text_at_cursor(code.strip_edges())




#============================================================
#   添加 NodePath
#============================================================

## 弹出 Dialog
func popup_dialog():
	dialog.clear()
	dialog.add_one()
	dialog.popup_centered_ratio(0.5)
	

## 添加节点路径
func add_node_path(name_list: Array):
	var code : String = ""
	var add_onready : bool = dialog.is_add_onready_var()
	var added : Array = []
	for n in name_list:
		n = n.strip_edges()
		if n and not added.has(n):
			added.append(n)
			code += "export var %s : NodePath \n" % n
			if add_onready:
				code += "onready var %s = get_node(%s) \n" % ["_" + n, n]
	
	# 插入到编辑器中
	var textedit = get_util().script_editor_util.get_current_code_textedit()
	if textedit:
		textedit.cursor_set_column(0)
		textedit.insert_text_at_cursor(code.strip_edges())
	else:
		printerr("当前没有打开代码编辑器")


